package com.aruba.navigation;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View button = getView().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Activity中绑定后，可以通过内部任意一个View获取NavController对象
                NavController navController = Navigation.findNavController(view);
//            Bundle bundle = new Bundle();
//            bundle.putString("name", "张三");
                HomeFragmentArgs build = new HomeFragmentArgs.Builder()
                        .setName("张三")
                        .setAge(1)
                        .build();
                //指定跳转的fragment，即navigation graph中的action
                navController.navigate(R.id.action_homeFragment_to_detailFragment, build.toBundle());
            }
        });
    }
}